package com.aidex.biz.smy.service;

import com.aidex.biz.domain.Customer;
import com.aidex.biz.service.CustomerService;
import com.aidex.biz.smy.bo.ClientBaseConfig;
import com.aidex.biz.smy.dto.BaseDTO;
import com.aidex.biz.smy.utils.NormalEncryptionUtil;
import com.aidex.biz.smy.vo.BaseVO;
import com.aidex.biz.smy.vo.Response;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xyb
 * @Description 相关处理
 * @since 15:36 2022/11/10
 */
@Service

@RequiredArgsConstructor
public class HitServiceImpl implements HitService {

    private static final Logger log = LoggerFactory.getLogger(HitServiceImpl.class);
    private final ClientBaseConfig baseConfig;

    @Autowired
    private CustomerService customerService;

    /**
     * 撞库处理
     *
     * @param param
     * @return
     */
    @Override
    public BaseVO handlerHit(BaseDTO param) {
        String result = NormalEncryptionUtil.responseDecrypt(JSONObject.toJSONString(param), baseConfig);
        log.info("解密结果值:{}", result);
        //TODO 处理自己这边的逻辑,过程中出现异常自己捕获，按照接口文档响应字段即可
        //撞库结果【是否命中】
        Boolean hit = true;
        Response response = Response.builder().code("000000").build();
        try {
            JSONObject jsonObject = JSONObject.parseObject(result);
            Customer customer = new Customer();
            customer.setPhoneMD5(jsonObject.getString("phoneNo"));
            hit = customerService.findCustomerByphone(customer);
            response = Response.builder().code("000000").build();
            if (!hit) {
                response.setDealStatus("0000");
                response.setDealDesc("准入成功");
            } else {
                response.setDealStatus("9999");
                response.setDealDesc("准入拒绝");
            }
        } catch (Exception e) {
            response.setDealStatus("9999");
            response.setDealDesc(e.getMessage());
            log.info("撞库/解密错误:{}", e.getMessage());
        }
        return NormalEncryptionUtil.requestEncryption(JSONObject.toJSONString(response), baseConfig, "3.1");
    }

    @Override
    public BaseVO handlerCommit(BaseDTO param) {
        String result = NormalEncryptionUtil.responseDecrypt(JSONObject.toJSONString(param), baseConfig);
        log.info("解密结果值:{}", result);
        //TODO 处理自己这边的逻辑,过程中出现异常自己捕获，按照接口文档响应字段即可
        JSONObject jsonObject = JSONObject.parseObject(result);
        Response response = null;
        try {
            Customer customer = new Customer();
            customer.setLaiyuan("大鹅推");
            customer.setClueId(jsonObject.getString("custId"));
            customer.setCusName(jsonObject.getString("custName"));
            customer.setSex(jsonObject.getString("gender").equals("00") ? "女" : "男");
            customer.setPhone(jsonObject.getString("phoneNo"));
            customer.setCityName(jsonObject.getString("addressCity"));
            customer.setAge(jsonObject.getInteger("age"));
            customer.setAmount(jsonObject.getString("limit"));
            JSONObject extraContent = jsonObject.getJSONObject("extraContent");
            if (null != extraContent) {
                customer.setCarNo(extraContent.getString("carId"));
                customer.setCarStatus(extraContent.getString("pledge").equals("1") ? "已抵押" : "未抵押");
                customer.setZx(extraContent.getString("creditType"));
                customer.setPgPrice(extraContent.getString("carMoney"));
                customer.setRemark(customer.getAmount() + "/" + customer.getZx() + "/" + customer.getCarStatus());
            }
            customerService.savePushCustomer(customer);
            response = Response.builder().code("000000").dealDesc("提交成功").dealStatus("0000").build();
        } catch (Exception e) {
            response = Response.builder().code("000000").dealDesc("推送失败").dealStatus("9999").build();
            log.info("提交信息/解密错误:" + e.getMessage());
        }
        return NormalEncryptionUtil.requestEncryption(JSONObject.toJSONString(response), baseConfig, "3.1");
    }

    @Override
    public BaseVO commitBelong(BaseDTO param) {
        String result = NormalEncryptionUtil.responseDecrypt(JSONObject.toJSONString(param), baseConfig);
        log.info("解密结果值:{}", result);
        //TODO 处理自己这边的逻辑,过程中出现异常自己捕获，按照接口文档响应字段即可
        Response response = Response.builder().code("000000").dealDesc("提交成功").dealStatus("0000").build();
        return NormalEncryptionUtil.requestEncryption(JSONObject.toJSONString(response), baseConfig, "3.1");
    }
}
